package life.upmenu;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import life.upmenu.View.MyAdapter;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bt)
    Button bt;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.activity_main)
    RelativeLayout activityMain;
    @BindView(R.id.rv)
    RecyclerView rv;
    private ObjectAnimator upTranslationY;
    private ObjectAnimator downTranslationY;
    private boolean isUp = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int heightPixels = displayMetrics.heightPixels;

        //得到ll的高度，990
        int height = getHeight();
        Toast.makeText(this, height + "", Toast.LENGTH_SHORT).show();

        //设置ll在y轴上向下移的距离
        ll.setY(heightPixels - 400);

        //设置动画
        ObjectAnimator objectAnimator = new ObjectAnimator();
        upTranslationY = objectAnimator.ofFloat(ll, "translationY", heightPixels - 400, heightPixels -(int)(height * 7.5) - 100, heightPixels - (int)(height * 7.5) + 100, heightPixels - (int)(height * 7.5));
        downTranslationY = objectAnimator.ofFloat(ll, "translationY", heightPixels - (int)(height * 7.5), heightPixels - 400);
        upTranslationY.setDuration(1000);
        downTranslationY.setDuration(700);

        //添加recyclerview
        addRecyclerview();
    }

    private void addRecyclerview() {
        //图片集合
        int[] imageviews = {R.mipmap.ball_left, R.mipmap.ball_right, R.mipmap.ball_left2, R.mipmap.ball_right2, R.mipmap.ball_left3, R.mipmap.ball_right3, R.mipmap.ball_left4, R.mipmap.ball_right4,
        R.mipmap.shad_left, R.mipmap.shad_right, R.mipmap.shad_left2, R.mipmap.shad_right2, R.mipmap.shad_left3, R.mipmap.shad_right3, R.mipmap.shad_left4,R.mipmap.shad_right4};

        RecyclerView.Adapter myAdapter = new MyAdapter(this, imageviews);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(myAdapter);
    }

    @OnClick(R.id.bt)
    public void onViewClicked() {
        if (isUp) {
            upTranslationY.start();
        } else {
            downTranslationY.start();
        }
        isUp = !isUp;
    }

    public int getHeight() {
        //第一种方式
        final int[] llHeight = new int[1];
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        ll.measure(w, h);
        int height = ll.getMeasuredHeight();

        //第二种方式
        ViewTreeObserver vto = ll.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
//                vto.removeOnPreDrawListener(this);
                llHeight[0] = ll.getMeasuredHeight();
                int width = ll.getMeasuredWidth();
                return true;
            }
        });


//        return llHeight[0];
        return height;
    }
}
